using System;
using Gtk;



namespace EstimatorCP01
{
	public enum rowType { trd = 1, mtr, mhn, usl};
	
	[System.ComponentModel.ToolboxItem(true)]
	public partial class anaWidget : Gtk.Bin
	{
		TreeStore store = new TreeStore(
		                                	typeof (string), typeof (string), typeof (string), typeof (string),
											typeof (string), typeof (string), typeof (string), typeof (string)
		                                );
		
		_defineTable defineTable;
		_editTable editTable;
		
		ValidationLib.myLib _v = new ValidationLib.myLib();
		
		//string с анализ
		public string source = "";
		
		//преки разходи
		public string trd = "", mtr = "", mhn = "", usl = "";
		
		//допълнителни разходи
		public string _trd = "", _mtr = "", _mhn = "", _usl = "";
		
		//сумарни стойности
		public string prk = "", dop = "", sum = "";
		
		//други
		public string osnovanie = "", komentar = "", Nvr = "";
		
		
		public anaWidget ()
		{
			this.Build ();
			
			defineTable = new _defineTable(store, treeview1);
			editTable = new _editTable(store, treeview1, defineTable);
			
			
			treeview1.ExpandAll();
			
			
//			this.SetProperty("Source", new GLib.Value(source));
//			
//			this.SetProperty("trd", new GLib.Value(_trd));
//			this.SetProperty("mtr", new GLib.Value(_mtr));
//			this.SetProperty("mhn", new GLib.Value(_mhn));
//			this.SetProperty("usl", new GLib.Value(_usl));
			
			
		}
		
		public void refresh()
		{
			defineTable.update();
			
			//Преки разходи
			trd = defineTable.TRD;
			mtr = defineTable.MTR;
			mhn = defineTable.MHN;
			usl = defineTable.USL;
			
			//Допълнителни разходи
			_trd = defineTable._TRD;
			_mtr = defineTable._MTR;
			_mhn = defineTable._MHN;
			_usl = defineTable._USL;
			
			//Сумарни стойности
			prk = defineTable.PRK;
			dop = defineTable.DOP;
			sum = defineTable.SUM;
			
			//Други
			osnovanie = entry1.Text;
			komentar  = entry2.Text;
			Nvr = defineTable.NVR;
			
			
		}
		
		public void setSource(string _in)
		{
			try{
				
				store.Clear();
				
				defineTable._model(store);
				
				refresh();
				
				source = _in;
				
				entry1.Text = "";
				entry2.Text = "";
				
				string[] part = _in.Split('@');
				
				string[] percents = part[2].Split('|');
				
				string[] row = part[1].Split('$');
				
				
				//Добавяне на редове
				
				for(int i = 0; i < row.Length; i++)
				{
					try{
						
						TreeIter _part;
						rowType _type = rowType.trd;
						
						string[] _value = row[i].Split('^');
						
						if( _value[7] == "trd") { store.IterNthChild(out _part, 0); _type = rowType.trd; };
						if( _value[7] == "mtr") { store.IterNthChild(out _part, 1); _type = rowType.mtr; };
						if( _value[7] == "mhn") { store.IterNthChild(out _part, 2); _type = rowType.mhn; };
						if( _value[7] == "usl") { store.IterNthChild(out _part, 3); _type = rowType.usl; };
						
						editTable.addRow(_type, _value[0], _value[1], _value[2], _value[3], _value[4], _value[5]);
						
					}catch{}
				}
			
				//Добавяне на проценти
				TreeIter _per, _main;
				store.IterNthChild(out _main, 4);
				
				for(int k = 0; k < 4; k++)
				{
					store.IterNthChild(out _per, _main, k);
					
					store.SetValue(_per, 3, _v.formatString(percents[k], 0));
				}
				
				
				
				//Добавяне на допълнителна информация
				entry1.Text = part[0].Split('|')[0];
				entry2.Text = part[0].Split('|')[1];
				
				defineTable.update();
				
				treeview1.ExpandAll();
				
			}catch{
				
				store.Clear();
				
				defineTable._model(store);
				
				refresh();
			}
		}
		
		public string getSource()
		{
			try{
				
				TreeIter _it;
				TreeIter _row;
				
				store.GetIterFirst(out _it);
				
				string[] part = new string[] { "", "", "" };
				
				
				//Взимане на основни редове
				for(int i = 0; i < 4; i++)
				{
					
						
					for(int j = 0; j < store.IterNChildren(_it); j++)
					{
						store.IterNthChild(out _row, _it, j);	
						
						part[1] += (string) store.GetValue(_row, 0) + "^";
						part[1] += (string) store.GetValue(_row, 1) + "^";
						part[1] += (string) store.GetValue(_row, 2) + "^";
						part[1] += (string) store.GetValue(_row, 3) + "^";
						part[1] += (string) store.GetValue(_row, 4) + "^";
						part[1] += (string) store.GetValue(_row, 5) + "^";
						part[1] += (string) store.GetValue(_row, 6) + "^";
						part[1] += (string) store.GetValue(_row, 7);
						
						part[1] += "$";
						
					}
					
					
					
					store.IterNext(ref _it);
							
				}
				
				
				
				//Взимане на проценти
				for(int k = 0; k < 4; k++)
				{
					store.IterNthChild(out _row, _it, k);
					
					part[2] += (string) store.GetValue(_row, 3) + "|";
					
				}
					
				//Взимане на основание и белжки
				part[0] = entry1.Text + "|" + entry2.Text;
				
				
				part[1] = part[1].Remove(part[1].Length - 1);
				part[2] = part[2].Remove(part[2].Length - 1);
				
				
				source = string.Join("@", part);
			
			}catch{}
			return source;			
		}	
			
		protected virtual void OnButton2Clicked (object sender, System.EventArgs e)
		{
			editTable.addRow(rowType.trd, "-", "-", "-", "1", "1", "1");
		}
		
		protected virtual void OnButton8Clicked (object sender, System.EventArgs e)
		{
			editTable.addRow(rowType.mtr, "-", "-", "-", "1", "1", "1");
		}
		
		protected virtual void OnButton9Clicked (object sender, System.EventArgs e)
		{
			editTable.addRow(rowType.mhn, "-", "-", "-", "1", "1", "1");
		}
		
		protected virtual void OnButton10Clicked (object sender, System.EventArgs e)
		{
			editTable.addRow(rowType.usl, "-", "-", "-", "1", "1", "1");
		}
		
		protected virtual void OnButton7Clicked (object sender, System.EventArgs e)
		{
			
			defineTable.update();
			
			_trd = defineTable.TRD;
			_mtr = defineTable.MTR;
			_mhn = defineTable.MHN;
			_usl = defineTable.USL;
		}
		
		protected virtual void OnButton3Clicked (object sender, System.EventArgs e)
		{
			editTable.deleteRow();
		}
		
		protected virtual void OnButton4Clicked (object sender, System.EventArgs e)
		{
			editTable.cutRow();
		}
		
		protected virtual void OnButton5Clicked (object sender, System.EventArgs e)
		{
			editTable.copyRow();
		}
		
		protected virtual void OnButton6Clicked (object sender, System.EventArgs e)
		{
			editTable.pasteRow();
		}
		
		
		
		
		
		
		
		
		
		
		
		
	}
	
	public class _defineTable
	{
		public TreeViewColumn colShifar			= new TreeViewColumn();	//1
		public TreeViewColumn colOpisanie		= new TreeViewColumn();	//2
		public TreeViewColumn colMka			= new TreeViewColumn();	//3
		public TreeViewColumn colRN				= new TreeViewColumn();	//4
		public TreeViewColumn colK				= new TreeViewColumn();	//5
		public TreeViewColumn colPrice			= new TreeViewColumn();	//6
		public TreeViewColumn colSum			= new TreeViewColumn();	//7
		public TreeViewColumn colType			= new TreeViewColumn();	//8
		
		public CellRendererText cellShifar		= new CellRendererText();
		public CellRendererText cellOpisanie	= new CellRendererText();
		public CellRendererText cellMka			= new CellRendererText();
		public CellRendererText cellRN			= new CellRendererText();
		public CellRendererText cellK			= new CellRendererText();
		public CellRendererText cellPrice		= new CellRendererText();
		public CellRendererText cellSum			= new CellRendererText();
		public CellRendererText cellType		= new CellRendererText();
		
		public TreeView tree;
		public TreeStore store;
		
		ValidationLib.myLib _v = new ValidationLib.myLib();
		
		
		
		public _defineTable(TreeStore _store, TreeView _tree)
		{
			colShifar.PackStart		(cellShifar,	true);	//1
			colOpisanie.PackStart	(cellOpisanie,	true);	//2
			colMka.PackStart		(cellMka,		true);	//3
			colRN.PackStart			(cellRN,		true);	//4
			colK.PackStart			(cellK,			true);	//5
			colPrice.PackStart		(cellPrice,		true);	//6
			colSum.PackStart		(cellSum,		true);	//7
			colType.PackStart		(cellType,		true);	//8
			
			colShifar.AddAttribute		(cellShifar,	"text", 0);
			colOpisanie.AddAttribute	(cellOpisanie,	"text", 1);
			colMka.AddAttribute			(cellMka,		"text", 2);
			colRN.AddAttribute			(cellRN,		"text", 3);
			colK.AddAttribute			(cellK,			"text", 4);
			colPrice.AddAttribute		(cellPrice,		"text", 5);
			colSum.AddAttribute			(cellSum,		"text", 6);
			colType.AddAttribute		(cellType,		"text", 7);
			
			_tree.AppendColumn(colShifar);
			_tree.AppendColumn(colOpisanie);
			_tree.AppendColumn(colMka);
			_tree.AppendColumn(colRN);
			_tree.AppendColumn(colK);
			_tree.AppendColumn(colPrice);
			_tree.AppendColumn(colSum);
			_tree.AppendColumn(colType);
		
			_tree.Model = _store;

			colOpisanie.SetCellDataFunc(cellOpisanie, new Gtk.TreeCellDataFunc (RenderKlon));
			colRN.SetCellDataFunc(cellRN, new Gtk.TreeCellDataFunc (RenderRN));
			colSum.SetCellDataFunc(cellSum, new Gtk.TreeCellDataFunc (RenderSum));
			
			formatTable();
			
			_model(_store);
			
			_tree.ExpandAll();
			
			tree = _tree;
			store = _store;
		}
		

		
		public void formatTable(){
			colShifar.Title = "Шифър";
			colShifar.MinWidth = 100;
			cellShifar.CellBackground = "#EEEEEE";
			
			colOpisanie.Title = "Позиция";
			colOpisanie.Resizable = true;
			colOpisanie.MinWidth = 300;
			
			colMka.Title = "м-ка";
			colMka.Resizable = true;
			colMka.MinWidth = 40;
						
			colRN.Title = "РН";
			colRN.Resizable = true;
			colRN.MinWidth = 55;
			cellRN.Xalign = 1;
			
			colK.Title = "К";
			colK.Resizable = true;
			colK.MinWidth = 55;
			cellK.Xalign = 1;
			
			colPrice.Title = "Цена";
			colPrice.Resizable = true;
			colPrice.MinWidth = 55;
			cellPrice.Xalign = 1;
			
			colSum.Title = "Общо";
			colSum.Resizable = true;
			colSum.MinWidth = 55;
			cellSum.CellBackground = "#EEEEEE";
			cellSum.Xalign = 1;
			
			colType.Title = "Тип";
			colType.Alignment = 1;
			cellType.Xalign = 1;
			cellType.Foreground = "Gray";
			
			//colType.Visible = false;
			
			//		cellShifar			0
			//		cellOpisanie		1
			//		cellMka				2
			//		cellRN				3
			//		cellK				4
			//		cellPrice			5
			//		cellSum - ne
			//		cellType - ne
			
			//Редактиране
			cellShifar.Editable = true;
			cellShifar.Edited += cellShifar_Edited;
			
			cellOpisanie.Editable = true;
			cellOpisanie.Edited += cellOpisanie_Edited;
			
			cellMka.Editable = true;
			cellMka.Edited += cellMka_Edited;
			
			cellRN.Editable = true;
			cellRN.Edited += cellRN_Edited;
			
			cellK.Editable = true;
			cellK.Edited += cellK_Edited;
			
			cellPrice.Editable = true;
			cellPrice.Edited += cellPrice_Edited;
		}
		
		public void _model(TreeStore _store){
			_store.Clear();
			
			_store.AppendValues("", "Труд", "", "", "", "", "", "");
			_store.AppendValues("", "Материали", "", "", "", "", "", "");
			_store.AppendValues("", "Механизация", "", "", "", "", "", "");
			_store.AppendValues("", "Услуги", "", "", "", "", "", "");
			
			
			_store.AppendValues("", "Допълнителни разходи", "", "", "", "", "", "");
			TreeIter _it = new TreeIter();
			_store.IterNthChild(out _it, 4);
			_store.AppendValues(_it, "", " - върху Труд", "%", "10", "", "", "", "");
			_store.AppendValues(_it, "", " - върху Материали (ДСР)", "%", "10", "", "", "", "");
			_store.AppendValues(_it, "", " - върху Механизация", "%", "10", "", "", "", "");
			_store.AppendValues(_it, "", " - върху Услуги", "%", "10", "", "", "", "");
			
			_store.AppendValues("", "Едицинична цена", "", "", "", "", "", "");
			_store.IterNthChild(out _it, 5);
			_store.AppendValues(_it, "", " - преки разходи", "лв", "", "", "", "", "");
			_store.AppendValues(_it, "", " - допълнителни разходи", "лв", "", "", "", "", "");
			
		}
		
		private void RenderKlon (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
		{
			string type = (string) model.GetValue (iter, 1);	
	 
			if ((type == "Труд") || (type == "Материали") || (type == "Механизация") || (type == "Услуги") || (type == "Допълнителни разходи") || (type == "Едицинична цена") ){
				(cell as Gtk.CellRendererText).Weight = 700;// Pango.Weight.Bold;

			} else {
				(cell as Gtk.CellRendererText).Weight = 400;
			}
	 
			(cell as Gtk.CellRendererText).Text = type;
		
		}
		
		private void RenderRN (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
		{
			string type = (string) model.GetValue (iter, 1);
			string rn = (string) model.GetValue (iter, 3);
	 
			if ((type == "Труд") || (type == "Материали") || (type == "Механизация") || (type == "Услуги") || (type == "Допълнителни разходи") || (type == "Едицинична цена")  ){
				(cell as Gtk.CellRendererText).Weight = 700;// Pango.Weight.Bold;

			} else {
				(cell as Gtk.CellRendererText).Weight = 400;
			}
	 
			(cell as Gtk.CellRendererText).Text = rn;
		
		}
		
		private void RenderSum (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
		{
			string type = (string) model.GetValue (iter, 1);	
			string sum = (string) model.GetValue (iter, 6);
	 
			if ((type == "Труд") || (type == "Материали") || (type == "Механизация") || (type == "Услуги") || (type == "Допълнителни разходи") || (type == "Едицинична цена") ){
				(cell as Gtk.CellRendererText).Weight = 700;// Pango.Weight.Bold;

			} else {
				(cell as Gtk.CellRendererText).Weight = 400;
			}
	 
			(cell as Gtk.CellRendererText).Text = sum;
		
		}
		
		
		
		//Клетки за редактиране - Шифър, Име, м-ка, РН, К, Ед ц
		
//		cellShifar			0
//		cellOpisanie		1
//		cellMka				2
//		cellRN				3
//		cellK				4
//		cellPrice			5
//		cellSum - ne
//		cellType - ne
		
		private void cellShifar_Edited (object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			
			tree.Selection.GetSelected(out iter);
		
			bool cond = (	(string) store.GetValue(iter, 1) != "Труд" )	&&
						(	(string) store.GetValue(iter, 1) != "Материали" )	&&
						(	(string) store.GetValue(iter, 1) != "Механизация" )	&&
						(	(string) store.GetValue(iter, 1) != "Услуги" )	&&
					
						(	(string) store.GetValue(iter, 1) != "Допълнителни разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Труд" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Материали (ДСР)" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Механизация" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Услуги" ) &&
					
						(	(string) store.GetValue(iter, 1) != "Едицинична цена" ) &&
						(	(string) store.GetValue(iter, 1) != " - преки разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - допълнителни разходи" );
			
			if(cond) store.SetValue(iter, 0, args.NewText);
		}
		
		private void cellOpisanie_Edited (object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			
			tree.Selection.GetSelected(out iter);
		
			bool cond = (	(string) store.GetValue(iter, 1) != "Труд" )	&&
						(	(string) store.GetValue(iter, 1) != "Материали" )	&&
						(	(string) store.GetValue(iter, 1) != "Механизация" )	&&
						(	(string) store.GetValue(iter, 1) != "Услуги" )	&&
					
						(	(string) store.GetValue(iter, 1) != "Допълнителни разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Труд" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Материали (ДСР)" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Механизация" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Услуги" ) &&
					
						(	(string) store.GetValue(iter, 1) != "Едицинична цена" ) &&
						(	(string) store.GetValue(iter, 1) != " - преки разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - допълнителни разходи" );
			
			if(cond) store.SetValue(iter, 1, args.NewText);
		}
		
		private void cellMka_Edited (object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			
			tree.Selection.GetSelected(out iter);
		
			bool cond = (	(string) store.GetValue(iter, 1) != "Труд" )	&&
						(	(string) store.GetValue(iter, 1) != "Материали" )	&&
						(	(string) store.GetValue(iter, 1) != "Механизация" )	&&
						(	(string) store.GetValue(iter, 1) != "Услуги" )	&&
					
						(	(string) store.GetValue(iter, 1) != "Допълнителни разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Труд" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Материали (ДСР)" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Механизация" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Услуги" ) &&
					
						(	(string) store.GetValue(iter, 1) != "Едицинична цена" ) &&
						(	(string) store.GetValue(iter, 1) != " - преки разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - допълнителни разходи" );
			
			if(cond) store.SetValue(iter, 2, args.NewText);
			
		}
		
		private void cellRN_Edited (object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			
			tree.Selection.GetSelected(out iter);
		
			bool cond = (	(string) store.GetValue(iter, 1) != "Труд" )	&&
						(	(string) store.GetValue(iter, 1) != "Материали" )	&&
						(	(string) store.GetValue(iter, 1) != "Механизация" )	&&
						(	(string) store.GetValue(iter, 1) != "Услуги" )	&&
					
						(	(string) store.GetValue(iter, 1) != "Допълнителни разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Труд" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Материали (ДСР)" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Механизация" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Услуги" ) &&
					
						(	(string) store.GetValue(iter, 1) != "Едицинична цена" ) &&
						(	(string) store.GetValue(iter, 1) != " - преки разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - допълнителни разходи" );
			
			bool c2 =	(	(string) store.GetValue(iter, 1) == " - върху Труд" ) ||
						(	(string) store.GetValue(iter, 1) == " - върху Материали (ДСР)" ) ||
						(	(string) store.GetValue(iter, 1) == " - върху Механизация" ) ||
						(	(string) store.GetValue(iter, 1) == " - върху Услуги" );
			
			if(cond && _v.validateDouble(args.NewText))
			{
				store.SetValue(iter, 3, _v.formatString( args.NewText, 4));
			} else
				if(c2 && _v.validateDouble(args.NewText)) store.SetValue(iter, 3, _v.formatString( args.NewText, 0));
			
			//_update(iter);
			update();
		}
		
		private void cellK_Edited (object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			
			tree.Selection.GetSelected(out iter);
		
			bool cond = (	(string) store.GetValue(iter, 1) != "Труд" )	&&
						(	(string) store.GetValue(iter, 1) != "Материали" )	&&
						(	(string) store.GetValue(iter, 1) != "Механизация" )	&&
						(	(string) store.GetValue(iter, 1) != "Услуги" )	&&
					
						(	(string) store.GetValue(iter, 1) != "Допълнителни разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Труд" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Материали (ДСР)" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Механизация" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Услуги" ) &&
					
						(	(string) store.GetValue(iter, 1) != "Едицинична цена" ) &&
						(	(string) store.GetValue(iter, 1) != " - преки разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - допълнителни разходи" );
			
			if(cond && _v.validateDouble(args.NewText)) store.SetValue(iter, 4, _v.formatString(args.NewText, 2));
			update();
		}
		
		private void cellPrice_Edited (object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			
			tree.Selection.GetSelected(out iter);
		
			bool cond = (	(string) store.GetValue(iter, 1) != "Труд" )	&&
						(	(string) store.GetValue(iter, 1) != "Материали" )	&&
						(	(string) store.GetValue(iter, 1) != "Механизация" )	&&
						(	(string) store.GetValue(iter, 1) != "Услуги" )	&&
					
						(	(string) store.GetValue(iter, 1) != "Допълнителни разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Труд" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Материали (ДСР)" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Механизация" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Услуги" ) &&
					
						(	(string) store.GetValue(iter, 1) != "Едицинична цена" ) &&
						(	(string) store.GetValue(iter, 1) != " - преки разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - допълнителни разходи" );
			
			if(cond && _v.validateDouble(args.NewText)) store.SetValue(iter, 5, _v.formatString( args.NewText, 2));
			update();
		}
		
		
		
		//Актуализация
		public void _update(TreeIter _it)
		{
			double rn = _v.GetDouble ( (string) store.GetValue(_it, 3 ));
			double ka = _v.GetDouble ( (string) store.GetValue(_it, 4 ));
			double pr = _v.GetDouble ( (string) store.GetValue(_it, 5 ));
			
			double sum = rn * ka * pr;
			
			TreeIter iter = new TreeIter();
			tree.Selection.GetSelected(out iter);
			
			bool cond =	(	(string) store.GetValue(iter, 1) != "Труд" )	&&
						(	(string) store.GetValue(iter, 1) != "Материали" )	&&
						(	(string) store.GetValue(iter, 1) != "Механизация" )	&&
						(	(string) store.GetValue(iter, 1) != "Услуги" )	&&
					
						(	(string) store.GetValue(iter, 1) != "Допълнителни разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Труд" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Материали (ДСР)" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Механизация" ) &&
						(	(string) store.GetValue(iter, 1) != " - върху Услуги" ) &&
					
						(	(string) store.GetValue(iter, 1) != "Едицинична цена" ) &&
						(	(string) store.GetValue(iter, 1) != " - преки разходи" ) &&
						(	(string) store.GetValue(iter, 1) != " - допълнителни разходи" );
			
			
			if(cond) store.SetValue(_it, 6, _v.formatString(sum.ToString(), 2));
			
		}
		
		//преки разходи
		public string TRD = "", MTR = "", MHN = "", USL = "";
		
		//допълнителни разходи
		public string _TRD = "", _MTR = "", _MHN = "", _USL = "";
		
		//сумарни стойности
		public string PRK = "", DOP = "", SUM = "", NVR = "";
		
		public void update(){
		
			double trd = 0, mtr = 0, mhn = 0, usl = 0;
			double _trd = 0, _mtr = 0, _mhn = 0, _usl = 0;
			double preki = 0, dop = 0;
			double sum = 0;
			
			double nvr = 0;
			
			TreeIter _iter = new TreeIter();
			TreeIter _sub  = new TreeIter();
			
			//Актуализация на редове
			for(int i = 0; i < 4; i++){
				
				store.IterNthChild(out _iter, i);
				
				for(int n = 0; n < store.IterNChildren(_iter); n++){
					store.IterNthChild(out _sub, _iter, n);
					
					double rn = _v.GetDouble ( (string) store.GetValue(_sub, 3 ));
					double ka = _v.GetDouble ( (string) store.GetValue(_sub, 4 ));
					double pr = _v.GetDouble ( (string) store.GetValue(_sub, 5 ));
			
					string type = (string) store.GetValue(_sub, 7);
					
					double _sum = rn * ka * pr;
					
					store.SetValue(_sub, 6, _v.formatString(_sum.ToString(), 2));
					
					if(type == "trd") { trd += _sum; nvr += rn;		};
					if(type == "mtr") { mtr += _sum;				};
					if(type == "mhn") {	mhn += _sum;				};
					if(type == "usl") {	usl += _sum;				};
				}
			}
			
			//Актуализация на суми труд, мат...
			store.IterNthChild(out _iter, 0);	store.SetValue(_iter, 6, _v.formatString(trd.ToString(), 2));
			store.IterNthChild(out _iter, 1);	store.SetValue(_iter, 6, _v.formatString(mtr.ToString(), 2));
			store.IterNthChild(out _iter, 2);	store.SetValue(_iter, 6, _v.formatString(mhn.ToString(), 2));
			store.IterNthChild(out _iter, 3);	store.SetValue(_iter, 6, _v.formatString(usl.ToString(), 2));
			
			//Актуализация на допълнителни разходи
			store.IterNthChild(out _iter, 4);
			
			
			
			store.IterNthChild(out _sub, _iter, 0);	store.SetValue(_sub, 5, _v.formatString(trd.ToString(), 2));
			
			double __trd = _v.GetDouble((string) store.GetValue(_sub, 3));
			_trd = __trd / 100 * trd;
			
			store.SetValue(_sub, 6, _v.formatString(_trd.ToString(), 2));
			
			
			
			store.IterNthChild(out _sub, _iter, 1);	store.SetValue(_sub, 5, _v.formatString(trd.ToString(), 2));
			
			double __mtr = _v.GetDouble((string) store.GetValue(_sub, 3));
			_mtr = __mtr / 100 * mtr;
			
			store.SetValue(_sub, 6, _v.formatString(_mtr.ToString(), 2));
			
			
			
			store.IterNthChild(out _sub, _iter, 2);	store.SetValue(_sub, 5, _v.formatString(mhn.ToString(), 2));
			
			double __mhn = _v.GetDouble((string) store.GetValue(_sub, 3));
			_mhn = __mhn / 100 * mhn;
			
			store.SetValue(_sub, 6, _v.formatString(_mhn.ToString(), 2));
			
			
			
			
			store.IterNthChild(out _sub, _iter, 3);	store.SetValue(_sub, 5, _v.formatString(usl.ToString(), 2));
			
			double __usl = _v.GetDouble((string) store.GetValue(_sub, 3));
			_usl = __usl / 100 * usl;
			
			store.SetValue(_sub, 6, _v.formatString(_usl.ToString(), 2));
			
			
			dop = _trd + _mtr + _mhn + _usl;
			
			store.SetValue(_iter, 6, _v.formatString(dop.ToString(), 2));
			
			//Крайна сума
			store.IterNthChild(out _iter, 5);
			preki = trd + mtr + mhn + usl;
			
			sum = preki + dop;
			
			store.SetValue(_iter, 6, _v.formatString(sum.ToString(), 2));
			store.IterNthChild(out _sub, _iter, 0);	store.SetValue(_sub, 6, _v.formatString(preki.ToString(), 2));
			store.IterNthChild(out _sub, _iter, 1);	store.SetValue(_sub, 6, _v.formatString(dop.ToString(), 2));
			
			
			//ПРЕКИ РАЗХОДИ			
			TRD = _v.formatString(	trd.ToString(), 2);
			MTR = _v.formatString(	mtr.ToString(), 2);
			MHN = _v.formatString(	mhn.ToString(), 2);
			USL = _v.formatString(	usl.ToString(), 2);
			
			//ДОПЪЛНИТЕЛНИ РАЗХОДИ
			_TRD = _v.formatString(	_trd.ToString(), 2);
			_MTR = _v.formatString(	_mtr.ToString(), 2);
			_MHN = _v.formatString(	_mhn.ToString(), 2);
			_USL = _v.formatString(	_usl.ToString(), 2);
			
			//СУМАРНИ СТОЙНОСТИ
			PRK = _v.formatString(	preki.ToString(),	2);
			DOP = _v.formatString(	dop.ToString(),		2);
			SUM = _v.formatString(	sum.ToString(),		2);
			
			//Нвр
			NVR = _v.formatString(	nvr.ToString(),		2);
		}
		
		
	}

	public class _editTable
	{
		TreeStore store;
		TreeView tree;
		
		_defineTable def;
		
		ValidationLib.myLib _v = new ValidationLib.myLib();
		
		public _editTable (TreeStore _store, TreeView _tree, _defineTable _def){
			store = _store;
			tree = _tree;
			
			def = _def;
			
		}
		
		public void addRow(rowType _type, string _sh, string _name, string _mka, string _rn, string _k, string _pr){
			TreeIter _it = new TreeIter();
			
			string _ty = "";
			
			if(_type == rowType.trd) { store.IterNthChild(out _it, 0); _ty = "trd"; } ;
			if(_type == rowType.mtr) { store.IterNthChild(out _it, 1); _ty = "mtr"; } ;
			if(_type == rowType.mhn) { store.IterNthChild(out _it, 2); _ty = "mhn"; } ;
			if(_type == rowType.usl) { store.IterNthChild(out _it, 3); _ty = "usl"; } ;
			
			double sum = _v.GetDouble(_rn) * _v.GetDouble(_k) * _v.GetDouble(_pr);
			
			store.AppendValues(	_it,
			                   
			                   _sh, _name, _mka,
			                   
			                   _v.formatString(_rn, 4),
			                   _v.formatString(_k, 2),
			                   _v.formatString(_pr, 2),
			                   _v.formatString(sum.ToString(), 2),
			                   
			                   _ty
			                   
			                  );
			tree.ExpandAll();
			
			def.update();
		}
		
		public void deleteRow()
		{
			try
			{
				TreeIter _it;
				
				if(tree.Selection.GetSelected(out _it))
				{
				
					string check = (string) store.GetValue(_it, 7);
					
					if( (check == "trd") || (check == "mtr") || (check == "mhn") || (check == "usl") )
					{
						store.Remove(ref _it);	
					}
				}
				
			}catch{}
		}
		
		public void copyRow()
		{
			try
			{
				TreeIter _it;
				
				if(tree.Selection.GetSelected(out _it))
				{
				
					string check = (string) store.GetValue(_it, 7);
					
					if( (check == "trd") || (check == "mtr") || (check == "mhn") || (check == "usl") )
					{
						Clipboard clippy = Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));
				
						string cl = "[an:row]";
						cl += store.GetValue(_it, 00) + "#";
						cl += store.GetValue(_it, 01) + "#";
						cl += store.GetValue(_it, 02) + "#";
						cl += store.GetValue(_it, 03) + "#";
						cl += store.GetValue(_it, 04) + "#";
						cl += store.GetValue(_it, 05) + "#";
						cl += store.GetValue(_it, 06) + "#";
						cl += store.GetValue(_it, 07);
		
						clippy.Text = cl;
						
						
					}
				}
				
			}catch{}
		}
		
		public void cutRow()
		{
			try
			{
				TreeIter _it;
				
				if(tree.Selection.GetSelected(out _it))
				{
				
					string check = (string) store.GetValue(_it, 7);
					
					if( (check == "trd") || (check == "mtr") || (check == "mhn") || (check == "usl") )
					{
						Clipboard clippy = Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));
				
						string cl = "[an:row]";
						cl += store.GetValue(_it, 00) + "#";
						cl += store.GetValue(_it, 01) + "#";
						cl += store.GetValue(_it, 02) + "#";
						cl += store.GetValue(_it, 03) + "#";
						cl += store.GetValue(_it, 04) + "#";
						cl += store.GetValue(_it, 05) + "#";
						cl += store.GetValue(_it, 06) + "#";
						cl += store.GetValue(_it, 07);
		
						clippy.Text = cl;
						
						store.Remove(ref _it);
					}
				}
				
			}catch{}	
		}
		
		public void pasteRow()
		{
			try
			{

					
						
						
				Clipboard rowClip = Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));
				string row = rowClip.WaitForText();
				
				if(row.Contains("[an:row]")){
					row = row.Replace("[an:row]", "");
					
					string[] values = row.Split('#');
					
					rowType _type = new rowType();
					if(values[7] == "trd") _type = rowType.trd;
					if(values[7] == "mtr") _type = rowType.mtr;
					if(values[7] == "mhn") _type = rowType.mhn;
					if(values[7] == "usl") _type = rowType.usl;
					
					addRow(_type, values[0], values[1], values[2], values[3], values[4], values[5]);
					
				
				}
			
				
			}catch{}
		}
		
	}

}

